﻿using RenterHttpTest.Models;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace RenterHttpTest
{
    class Program
    {
        private static string apiURI = "http://localhost:3000/";
        private MediaTypeWithQualityHeaderValue mediaType = new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded");

        static void Main(string[] args)
        {
            Authentication();
            //GetPosts();
            GetPost();
        }
        
        public static void Authentication()
        {
            var stringContent = new StringContent("email=hanna%40test.com&password=hanna");
            dynamic response;
            using (HttpClient client = new HttpClient())
            {
                HttpClientBuilder(client);

                try
                {
                    HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "authentication");
                    req.Content = new StringContent("email=hanna@test.com&password=hanna", Encoding.UTF8, "application/x-www-form-urlencoded");

                    client.SendAsync(req).ContinueWith(respTask =>
                    {
                        response = respTask.Result.Headers;
                        foreach (var item in response)
                        {
                            Console.WriteLine("Item: {0}", item);
                        }
                        Console.WriteLine("Response: {0}", respTask.Result);
                    });

                    Console.ReadLine();


                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }

            
        }

        public static void GetPosts()
        {
            using (HttpClient client = new HttpClient())
            {
                HttpClientBuilder(client);

                try
                {
                    var response = client.GetAsync("posts");
                    if (response.Result.IsSuccessStatusCode)
                    {
                        var posts = response.Result.Content.ReadAsStringAsync();
                        Console.WriteLine("Response: {0}", posts.Result);
                    }

                    Console.ReadLine();
                }
                catch (HttpRequestException e)
                {
                    // TODO
                    throw e;
                }
            }
        }

        public static void GetPost()
        {
            using (HttpClient client = new HttpClient())
            {
                HttpClientBuilder(client);

                try
                {

                    HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, "posts/54515f53ccbd691c3686968d");
                    req.Headers.Add("Cookie", "connect.sid=s%3ARW_rQXaob22fMUReBfyDqENioq2npyzU.3D9XFCvHdwrqPuIE7TFgcn9dLTyxJcx2CeIRIXzyb2E");

                    client.SendAsync(req).ContinueWith(respTask =>
                    {
                        Console.WriteLine("Response: {0}", respTask.Result);
                    });

                    Console.ReadLine();
                }
                catch (HttpRequestException e)
                {
                    // TODO
                    throw e;
                }
            }
        }

        private static void HttpClientBuilder(HttpClient client)
        {
            client.BaseAddress = new Uri(apiURI);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
        }
    }
}
